package com.py.credit.utils;

import java.math.BigInteger;

public class ByteUtil {
    public static void main(String[] args) {
        System.out.println("-----");
        byte[] b0 = hexStringToByte("79B44100");
        System.out.println(bytesToInt(b0, 0));
    }

    public static byte[] intToBytes(int value) {
        byte[] src = new byte[]{(byte)(value & 255), (byte)(value >> 8 & 255), (byte)(value >> 16 & 255), (byte)(value >> 24 & 255)};
        return src;
    }

    public static byte[] intToBytes2(int value) {
        byte[] src = new byte[]{(byte)(value >> 24 & 255), (byte)(value >> 16 & 255), (byte)(value >> 8 & 255), (byte)(value & 255)};
        return src;
    }

    public static int bytesToInt(byte[] src, int offset) {
        int value = src[offset] & 255 | (src[offset + 1] & 255) << 8 | (src[offset + 2] & 255) << 16 | (src[offset + 3] & 255) << 24;
        return value;
    }

    public static int bytesToInt2(byte[] src, int offset) {
        int value = (src[offset] & 255) << 24 | (src[offset + 1] & 255) << 16 | (src[offset + 2] & 255) << 8 | src[offset + 3] & 255;
        return value;
    }

    public static int bytesToInt3(byte[] src, int offset) {
        int value = (src[offset] & 255) << 8 | src[offset + 1] & 255;
        return value;
    }

    public static int bytesToInt30(byte[] src, int offset) {
        int value = src[offset] & 255 | (src[offset + 1] & 255) << 8;
        return value;
    }

    public static int bytesToInt300(byte[] src, int offset) {
        int value = src[offset] & 255 | (src[offset + 1] & 255) << 8 | (src[offset + 2] & 255) << 16;
        return value;
    }

    public static int bytesToInt4(byte[] src, int offset) {
        int value = src[offset] & 255;
        return value;
    }

    public static byte[] hexStringToByte(String hex) {
        if (hex == null) {
            return null;
        } else {
            int len = hex.length() / 2;
            byte[] result = new byte[len];
            char[] achar = hex.toCharArray();

            for(int i = 0; i < len; ++i) {
                int pos = i * 2;
                result[i] = (byte)(toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
            }

            return result;
        }
    }

    private static byte toByte(char c) {
        byte b = (byte)"0123456789ABCDEF".indexOf(c);
        return b;
    }

    public static final String bytesToHexString(byte[] bArray) {
        StringBuffer sb = new StringBuffer(bArray.length);

        for(int i = 0; i < bArray.length; ++i) {
            String sTemp = Integer.toHexString(255 & bArray[i]);
            if (sTemp.length() < 2) {
                sb.append(0);
            }

            sb.append(sTemp.toUpperCase());
        }

        return sb.toString();
    }

    public static String binary(byte[] bytes, int radix) {
        return (new BigInteger(1, bytes)).toString(radix);
    }

    public static int bytesToInt(Byte[] bytes) {
        byte[] bytes1 = new byte[bytes.length];

        for(int i = 0; i < bytes1.length; ++i) {
            bytes1[i] = bytes[i];
        }

        return (new BigInteger(bytes1)).intValue();
    }

    public static byte[] byteMerger(byte[] bt1, byte[] bt2) {
        byte[] bt3 = new byte[bt1.length + bt2.length];
        System.arraycopy(bt1, 0, bt3, 0, bt1.length);
        System.arraycopy(bt2, 0, bt3, bt1.length, bt2.length);
        return bt3;
    }
}
